package advert_api

import (
	"github.com/gin-gonic/gin"
	"gvb_server/global"
	"gvb_server/models"
	"gvb_server/models/res"
)

type AdvertRequest struct {
	Title  string `json:"title" binding:"required" msg:"请输入标题" structs:"title"`         // 标题
	Href   string `json:"href" binding:"required,url" msg:"请输入跳转链接" structs:"href"`     // 跳转连接
	Images string `json:"images" binding:"required,url" msg:"请输入图片地址" structs:"images"` // 图片
	IsShow bool   `json:"is_show" binding:"required" msg:"请选择是否展示" structs:"is_show"`   // 是否展示
	Sort   uint   `json:"sort" binding:"required" msg:"请输入排序号" structs:"sort"`          // 排序号
}

// AdvertCreate 添加广告
// @Tags 广告管理
// @Summary 创建广告
// @Description 创建广告
// @Param data body AdvertRequest true "表示多个参数"
// @Router /api/advert [post]
// Produce json
// @Success 200 {object} res.Response{}
func (AdvertApi) AdvertCreate(c *gin.Context) {
	var ar AdvertRequest
	err := c.ShouldBindJSON(&ar)
	if err != nil {
		res.FailWithError(err, &ar, c)
		return
	}
	err = global.DB.Create(&models.AdvertModel{
		Title:  ar.Title,
		Href:   ar.Href,
		Images: ar.Images,
		IsShow: ar.IsShow,
		Sort:   ar.Sort,
	}).Error
	if err != nil {
		global.Log.Error(err)
		res.Fail("广告添加失败！", c)
	}
	res.OkWith(c)
}
